package com.rxnetwork.sample.utils.network;

import android.support.annotation.NonNull;

import com.rxnetwork.manager.RxNetWork;
import com.rxnetwork.sample.utils.interceptor.AppLogInterceptor;

import java.io.File;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscription;

/**
 * xiaocode
 */
public class AppNetWorkManager {
    private volatile static AppNetWorkManager singleton;

    private static MultipartBody multipartBody;

    private AppNetWorkManager() {
    }

    public static AppNetWorkManager getInstance() {
        if (singleton == null) {
            synchronized (AppNetWorkManager.class) {
                if (singleton == null) {
                    singleton = new AppNetWorkManager();
                }
            }
        }
        return singleton;
    }

    public AppNetWorkManager setBaseUrl(@NonNull String url) {
        RxNetWork
                .getInstance()
                .setBaseUrl(url);
        return this;
    }

    public static MultipartBody getMultipartBody() {
        return multipartBody;
    }

    /**
     * @param observable
     * @param listener   全状态回调
     * @param <T>
     */
    public <T> void getApi(Observable<T> observable, final AppNetWorkFullListener<T> listener) {
        baseRequest().getApi(observable, listener);
    }

    /**
     * @param observable
     * @param listener   只有成功和失败回调
     * @param <T>
     */
    public <T> void getApi(Observable<T> observable, final AppNetWorkListener<T> listener) {
        baseRequest().getApi(observable, listener);
    }

    public static <T> T observable(Class<T> service) {
        return RxNetWork
                .getInstance().getRetrofit().create(service);
    }

    /**
     * 多图上传
     *
     * @param fileKey 服务端接收图片的key
     * @param files   上传文件集合
     * @param parts   上传form表单参数
     * @return
     */
    public AppNetWorkManager setUploadFiles(String fileKey, List<File> files, Map<String, String> parts) {
        RequestBody requestBody;
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        for (int i = 0; i < files.size(); i++) {
            File file = files.get(i);
            if (file != null) {
                requestBody = RequestBody.create(
                        MediaType.parse("multipart/form-data"), file);
                builder.addFormDataPart(fileKey == null ? file.getName() : fileKey, file.getName(), requestBody);
            }
        }
        for (Map.Entry<String, String> entry : parts.entrySet()) {
            if (entry != null && builder != null)
                builder.addFormDataPart(entry.getKey(), entry.getValue());
        }
        multipartBody = builder.build();
        return this;
    }

    /**
     * 取消所有网络
     */
    public void clearSubscription() {
        RxNetWork.getInstance().clearSubscription();
    }

    private RxNetWork baseRequest() {
        RxNetWork
                .getInstance();
        return RxNetWork.getInstance();
    }
}
